<template>
  <div>
    <!-- 三级分类 -->
    <Category :sence="sence"></Category>
    <el-card style="margin: 10px 0px">
      <div v-show="sence == 0">
        <el-button @click="addSpu" :disabled="!categoryStore.c3Id" type="primary" size="default" icon="Plus"
          >添加SPU</el-button
        >
        <!-- 展示已有SPU数据 -->
        <el-table style="margin: 10px 0px" border :data="records">
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="80px"
          ></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column
            label="SPU描述"
            prop="description"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="SPU操作">
            <!-- row:即为已有的SPU对象 -->
            <template #="{ row, $index }">
              <el-button
                type="primary"
                size="small"
                icon="Plus"
                title="添加SKU"
                @click="addSku(row)"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                icon="Edit"
                title="修改SPU"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                icon="View"
                title="查看SKU列表"
                @click="checkSkuList(row.id)"
              ></el-button>
              <el-popconfirm :title="`你确定删除${row.spuName}?`" width="200px" @confirm="deleteSpu(row.id)">
                <template #reference>
                  <el-button
                    type="primary"
                    size="small"
                    icon="Delete"
                    title="删除SPU"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 7, 9]"
          :background="true"
          layout="prev, pager, next, jumper,->,sizes,total"
          :total="total"
          @current-change="getHasSpu"
          @size-change="changeSize"
        />
      </div>
      <!-- 添加SPU|修改SPU子组件 -->
      <SpuForm ref="spu" @changeSence="changeSence" v-show="sence == 1"></SpuForm>
      <!-- 添加SKU的子组件 -->
      <SkuForm ref="sku" @changeSence="changeSence" v-show="sence == 2"></SkuForm>
      <!-- dialog对话框:展示已有的SKU数据 -->
      <el-dialog v-model="show" title="SKU列表">
                <el-table :data="skuArr" border>
                    <el-table-column label="SKU名字" prop="skuName"></el-table-column>
                    <el-table-column label="SKU价格" prop="price"></el-table-column>
                    <el-table-column label="SKU重量" prop="weight"></el-table-column>
                    <el-table-column label="SKU图片">
                        <template #="{ row, $index }">
                            <img :src="row.skuDefaultImg" style="width: 100px;height: 100px;">
                        </template>
                    </el-table-column>
                </el-table>
            </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import {ref, watch } from "vue";
import { reqHasSpu,reqSkuList,reqRemoveSpu } from "@/api/product/spu/index.ts";
import SpuForm from './spuForm.vue'
import SkuForm from './skuForm.vue'
import type {HasSpuResponseData,SpuData,SkuInfoData,SkuData} from '@/api/product/spu/type'
import useCategoryStore from "@/store/modules/category";
import { ElMessage } from "element-plus";
let categoryStore = useCategoryStore();
let sence = ref<number>(0);
let pageNo = ref<number>(1);
let pageSize = ref<number>(3);
let total = ref<number>(0);
let records = ref<SpuData[]>([])
//获取子组件实例SpuForm
let spu = ref<any>();
let sku = ref<any>()
let show = ref<boolean>(false)
let skuArr = ref<SkuData[]>([])
watch(
  () => categoryStore.c3Id,
  () => {
    getHasSpu()
  }
);
// 获取某一个三级分类下全部的已有的SPU
const getHasSpu = async (page=1)=>{
  page = pageNo.value
  let result:HasSpuResponseData = await reqHasSpu(pageNo.value,pageSize.value,categoryStore.c3Id)
  if(result.code==200){
  records.value = result.data.records
  total.value = result.data.total
  }
}
// 添加spu
const addSpu = () => {
  sence.value = 1;
  spu.value.initAddSpu(categoryStore.c3Id)
};
// 修改spu
const updateSpu = (row:SpuData)=>{
  sence.value = 1;
  //调用子组件实例方法获取完整已有的SPU的数据
  spu.value.initHasSpuData(row)
}
const changeSize=()=>{
  getHasSpu()
}
const changeSence = (obj:any)=>{
  sence.value = obj.flag
  // 如果是修改的话修改后留在当前页，否则去第一页
  if(obj.type=='update'){
    getHasSpu(pageNo.value)
  }else{
    getHasSpu()
  }
}
// 添加sku
const addSku = (row:SpuData)=>{
  sku.value.initSkuData(categoryStore.c1Id,categoryStore.c2Id,row)
  sence.value = 2
}
// 查看sku数据
const checkSkuList = async (spuId:number)=>{
  let result:SkuInfoData = await reqSkuList(spuId)
  if(result.code==200){
    skuArr.value = result.data
    show.value = true
  }
}
// 删除spu
const deleteSpu = async (spuId:number)=>{
  let result = await reqRemoveSpu(spuId)
  if(result.code==200){
    ElMessage({
      type:'success',
      message:'删除成功'
    })
    getHasSpu(records.value.length>1?pageNo.value:pageNo.value-1)
  }else{
    ElMessage({
      type:'error',
      message:'删除失败'
    })
  }
}
</script>

<style lang="scss" scoped></style>
